package org.weasel.unittests;

import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Time;
import java.util.ArrayList;

import junit.framework.TestCase;

import org.weasel.business.Developer;
import org.weasel.business.Project;
import org.weasel.business.Release;
import org.weasel.business.Unscheduled;
import org.weasel.business.UserStory;
import org.weasel.business.Utilities;
import org.weasel.data.ProjectManager;

public class ProjectTest extends TestCase {
	private String location = "./testfolder/ProjectTest.xml";
	private ProjectManager projMan;
	private Project project;
	private ArrayList<Developer> allDevelopers;
	private Developer developer;
	
	@Override
	protected void setUp() throws Exception {
		super.setUp();
		resetXMLFile();
		developer = new Developer("9999999", "developer", "mail", "position");
		allDevelopers = new ArrayList<Developer>();
		allDevelopers.add(developer);
		project = new Project("123456", "myproject", location, allDevelopers);
		projMan = project.getProjectManager();
	}

	@Override
	protected void tearDown() throws Exception {
		super.tearDown();
		project = null;
		projMan = null;
		developer = null;
		allDevelopers = null;
		Utilities.deleteFile(location);
	}

	public void testDevelopers() throws Exception {
		project.addDeveloper(developer);
		ArrayList<String> developerIDs = projMan.getDeveloperIDs();
		
		assertTrue(developerIDs.contains(developer.getId()));
	}

	public void testRemoveDeveloper() throws Exception {
		project.removeDeveloper(developer.getId());
		ArrayList<String> developerIDs = projMan.getDeveloperIDs();
		
		assertFalse(developerIDs.contains(developer.getId()));
	}

	public void testReleases() throws Exception {
		Release release = project.createAndAddRelease("release 1");
		
		//check if in XML file also
		Release rCheck = projMan.getRelease(release.getId());
		
		assertEquals("Expected release (from XML) with id " + release.getId(), release.getId(), rCheck.getId());
		assertEquals("Expected release with id " + release.getId(), release.getId(), project.getRelease(release.getId()).getId());
	}

	public void testRemoveRelease() throws Exception {
		boolean ok = false;
		Release release = project.createAndAddRelease("release 1");
		
		project.removeRelease(release.getId());
		
		//check if present in xml file
		try{
			projMan.getRelease(release.getId());
		}catch (NullPointerException e){
			ok = true;
		}
		
		assertEquals("Expected that error is thrown and so boolean set to true", true, ok);
		assertEquals(null, project.getRelease(release.getId()));
	}
	
	public void testUnscheduledStories() throws Exception{
		UserStory story = project.addUnscheduledStory("my story", 5, Time.valueOf("13:00:00"), Time.valueOf("14:00:00"), 5);
		
		//check if it is present in the XML-file
		UserStory storyCheck = projMan.getUserStory(story.getId());
		
		assertEquals("expected story (from XML) with id " + story.getId(), story.getId(), storyCheck.getId());
		assertEquals("expected story id " + story.getId(), story.getId(), project.getUnscheduledStory(story.getId()).getId());
		assertEquals("expected story name 'my story'", story.getName(), project.getUnscheduledStory(story.getId()).getName());
		
		//check also unscheduled area of XML file...story should be there
		Unscheduled unsch = projMan.getUnscheduled();
		UserStory storyUnsch = unsch.getUnscheduledUserStory(story.getId());
		assertEquals("expected unscheduled story with id " + story.getId(), story.getId(), storyUnsch.getId());
	}
	
	public void testRemoveUnscheduledStory() throws Exception{
		boolean ok = false;
		UserStory story = project.addUnscheduledStory("my story", 5, Time.valueOf("13:00:00"), Time.valueOf("14:00:00"), 5);
		
		project.removeUnscheduledStory(story.getId());
		
		//should be any more in the XML file
		try{
			projMan.getUserStory(story.getId());
		}catch (NullPointerException e){
			ok = true;
		}
		assertEquals("expected true since story should be present any more", true, ok);
		assertEquals("expected a null value", null, project.getUnscheduledStory(story.getId()));
	}

	
	/**
	 * create basic structure of the XML-file for testing
	 * @throws IOException
	 */
	private void resetXMLFile() throws IOException{
		String xmlFileContent = "" +
				"<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n" +
				"	<project>\n" + 
				"		<developers></developers>\n" +
				"		<releases></releases>\n" +
				"		<unscheduled>\n" +
				"			<id>31411407294223</id>\n" +
				"			<userstories></userstories>\n" +
				"		</unscheduled>\n" +
				"	</project>";
		
		//create file writer that doesn't append on the file
		FileWriter f = new FileWriter(location, false);
		PrintWriter out = new PrintWriter(f); 
		out.println(xmlFileContent);
		out.close();
		f.close();
	}
}
